﻿package mortal.game.view.mining.render
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;

    public class MiningHelpOthersRender extends NoSkinCellRenderer
    {
        private var _iconBg:GBitmap;
        private var _icon:GImageBitmap;
        private var _lvBg:ScaleBitmap;
        private var _txtLv:GTextFiled;
        private var _txtName:GTextFiled;
        private var _txtType:GTextFiled;
        private var _txtTask:GTextFiled;
        private var _btnHelp:GLabelButton;
        private var _btnRefuse:GLabelButton;
        private var _secTimer:SecTimerView;
        private var _myData:STaskRoutineTaskOtherPlayersInfo;
        private var _timer:Timer;

        public function MiningHelpOthersRender()
        {
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._myData = param1 as STaskRoutineTaskOtherPlayersInfo;
            var _loc_2:* = this._myData.player;
            this._icon.imgUrl = AvatarUtil.getPlayerAvatarUrl(_loc_2.career, _loc_2.sex, AvatarUtil.Small);
            this._txtLv.text = _loc_2.level.toString();
            this._txtName.text = _loc_2.name;
            this._txtType.text = GameDefConfig.instance.getItem("EGuildPostion", this._myData.position).text;
            var _loc_3:* = Cache.instance.mining.getBeAskForHelpTime(_loc_2.entityId.id);
            var _loc_4:* = Math.ceil((_loc_3 - FrameManager.flashFrame.getTime() + 60000) / 1000);
            if (_loc_4 > 0)
            {
                this._secTimer.setLeftTime(_loc_4);
            }
            else
            {
                if (this._secTimer.running)
                {
                    this._secTimer.stop();
                }
                this.countdownEnd();
            }
            var _loc_5:* = ColorConfig.instance.getItemColor((this._myData.color - 1));
            if (this._myData.color == 0)
            {
                DisplayUtil.removeMe(this._txtTask);
            }
            else
            {
                this._txtTask.htmlText = Language.getStringByParam(21036, _loc_5.color, this._myData.beingHelp, GameConst.TRoutineTaskBeingRefreshCount);
            }
            if (this._myData.color == (EColor._EColorOrange + 1) || this._myData.taskcode <= 0 || this._myData.color == 0 || !Cache.instance.mining.hasHelpOthersCount || this._myData.beingHelp >= GameConst.TRoutineTaskBeingRefreshCount)
            {
                this._btnHelp.mouseEnabled = false;
                this._btnHelp.mouseChildren = false;
                this._btnHelp.filters = [FilterConst.colorFilter];
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(0, 0, 283, 50, this, ImagesConst.MinigBg));
            this._iconBg = UIFactory.gBitmap(ImagesConst.shortcutItemBg, 2, 4, this);
            this._icon = UIFactory.gImageBitmap(null, this._iconBg.x + 2, this._iconBg.y + 2, this);
            this._lvBg = UIFactory.bg(2, 27, 27, 19, this, ImagesConst.LevelBg);
            this._txtLv = UIFactory.gTextField("10", -5, this._lvBg.y, 42, 20, this, GlobalStyle.textFormatPutong.center());
            this._txtName = UIFactory.gTextField("", 51, 3, 100, 20, this);
            this._txtName.textColor = 65280;
            this._txtType = UIFactory.gTextField("[]", 51, this._txtName.y + 21, 100, 20, this);
            this._txtTask = UIFactory.gTextField("", 129, 16, 100, 20, this);
            this._btnHelp = UIFactory.gLabelButton(null, GLabelButton.gButton, "MiningButton", 212, 2, 60, 22, this);
            this._btnHelp.labelDx = -10;
            this._secTimer = UIFactory.secTimeView("[ss]", this._btnHelp.x + 31, (this._btnHelp.y - 1), 80, 20, this);
            this._secTimer.textColor = 65280;
            this._secTimer.timeOutHandler = this.countdownEnd;
            this._btnRefuse = UIFactory.gLabelButton(null, GLabelButton.gButton, "MiningButton", 212, this._btnHelp.y + 24, 60, 22, this);
            LoaderHelp.addResCallBack(ResFileConst.mining, this.resLoaded);
            this._btnHelp.configEventListener(MouseEvent.CLICK, this.clickHelpHandler);
            this._btnRefuse.configEventListener(MouseEvent.CLICK, this.clickRefuseHandler);
            return;
        }// end function

        private function countdownEnd() : void
        {
            this._btnHelp.mouseEnabled = false;
            this._btnHelp.mouseChildren = false;
            this._btnHelp.filters = [FilterConst.colorFilter];
            this._secTimer.stop();
            this._secTimer.htmlText = "<font color=\'#ff0000\'>[0]</font>";
            this._btnHelp.labelDx = -6;
            this._btnHelp.updateLayout();
            return;
        }// end function

        private function clickHelpHandler(event:MouseEvent) : void
        {
            this._btnHelp.mouseEnabled = false;
            this._btnHelp.mouseChildren = false;
            this._btnHelp.filters = [FilterConst.colorFilter];
            this._btnRefuse.mouseEnabled = false;
            this._btnRefuse.mouseChildren = false;
            this._btnRefuse.filters = [FilterConst.colorFilter];
            if (this._timer == null)
            {
                this._timer = new Timer(1200, 1);
                this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.timerHandler);
            }
            this._timer.start();
            GameProxy.task.helpOtherRrefreshMiningTask(this._myData.player.entityId.id);
            return;
        }// end function

        private function timerHandler(event:TimerEvent) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._btnHelp.mouseEnabled = true;
            this._btnHelp.mouseChildren = true;
            this._btnHelp.filters = [];
            this._btnRefuse.mouseEnabled = true;
            this._btnRefuse.mouseChildren = true;
            this._btnRefuse.filters = [];
            if (this._myData == null || this._myData.color == (EColor._EColorOrange + 1) || this._myData.taskcode <= 0 || this._myData.color == 0 || !Cache.instance.mining.hasHelpOthersCount || this._myData.beingHelp >= GameConst.TRoutineTaskBeingRefreshCount)
            {
                this._btnHelp.mouseEnabled = false;
                this._btnHelp.mouseChildren = false;
                this._btnHelp.filters = [FilterConst.colorFilter];
            }
            return;
        }// end function

        private function clickRefuseHandler(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MiningRefuseHelp, this._myData));
            return;
        }// end function

        private function resLoaded() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._btnHelp.label = ImagesConst.Mining_bg;
            this._btnRefuse.label = ImagesConst.Mining_jj;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._iconBg.dispose(param1);
            this._iconBg = null;
            this._icon.dispose(param1);
            this._icon = null;
            this._lvBg.dispose(param1);
            this._lvBg = null;
            this._txtLv.dispose(param1);
            this._txtLv = null;
            this._txtName.dispose(param1);
            this._txtName = null;
            this._txtType.dispose(param1);
            this._txtType = null;
            this._txtTask.dispose(param1);
            this._txtTask = null;
            this._btnHelp.dispose(param1);
            this._btnHelp.filters = [];
            this._btnHelp = null;
            this._btnRefuse.filters = [];
            this._btnRefuse.dispose(param1);
            this._btnRefuse = null;
            if (this._secTimer != null)
            {
                this._secTimer.stop();
                this._secTimer.dispose(param1);
                this._secTimer = null;
            }
            return;
        }// end function

    }
}
